package com.bo.threadMethod;

/**
 * @Author: gpb
 * @Date: 2024/7/14 11:19
 * @Description:
 */
public class ThreadWaitAndNotify {
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            sync();
        }, "T1");
        Thread t2 = new Thread(() -> {
            sync();
        }, "T2");
        t1.start();
        t2.start();
        Thread.sleep(12000);
        synchronized (ThreadWaitAndNotify.class) {
            ThreadWaitAndNotify.class.notifyAll();
        }
    }

    public synchronized static void sync() {
        for (int i = 0; i < 10; i++) {
            try {
                if (i == 5) {
                    ThreadWaitAndNotify.class.wait();
                }
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(Thread.currentThread().getName() + ": " + i);
        }
    }
}
